<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>1_非受控组件</title>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="root"></div>

    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom 用于支持react操作dom -->
    <script
      type="text/javascript"
      src="../js/react-dom.development.js"
    ></script>
    <!-- 引入babel 将js转换成jsx -->
    <script type="text/javascript" src="../js/babel.min.js"></script>

    <!-- type="text/babel" 表示写的是 jsx 使用babel 翻译 -->
    <script type="text/babel">
      // 1. 创建组件
      class Login extends React.Component {
        // 初始化状态
        state = {
          username: "",
          password: "",
        };
        // 保存用户名
        changeUserName = (e) => {
          this.setState({ username: e.target.value });
        };
        // 保存密码
        changePassword = (e) => {
          this.setState({ password: e.target.value });
        };
        // 表单提交
        handleSubmit = (e) => {
          e.preventDefault();
          const { username, password } = this.state;
          alert(`用户名是${username},密码是${password}`);
        };
        render() {
          return (
            <div>
              <form onSubmit={this.handleSubmit}>
                <span>账号:</span>
                <input
                  type="text"
                  onChange={this.changeUserName}
                  placeholder="请输入账号"
                  name="username"
                />
                <span>密码:</span>
                <input
                  type="text"
                  onChange={this.changePassword}
                  placeholder="请输入密码"
                  name="password"
                />
                <button>提交</button>
              </form>
            </div>
          );
        }
      }
      ReactDOM.render(<Login />, document.getElementById("root"));
    </script>
  </body>
</html>
